//
//  NSTimer+BZBlock.m
//  BZFoundation
//
//  Created by 郭艺伟 on 2017/10/1.
//  Copyright © 2017年 郭艺伟. All rights reserved.
//

#import "NSTimer+BZBlock.h"
@interface BZTimerBlockHandler : NSObject
@property (nonatomic, copy) HandleTimerBlock block;
- (void)bz_handleBlockWithTimer:(NSTimer *)timer;
@end

@implementation BZTimerBlockHandler

@synthesize block;

- (void)bz_handleBlockWithTimer:(NSTimer *)timer
{
    HandleTimerBlock callbackBlock = self.block;
    
    NSAssert(callbackBlock != nil, @"Timer run block is nil");
    
    callbackBlock();
}

@end

@implementation NSTimer (BZBlock)
+ (NSTimer *)bz_scheduledTimerWithTimeInterval:(NSTimeInterval)seconds block:(HandleTimerBlock)block repeats:(BOOL)inRepeats{
    
    BZTimerBlockHandler *blockHandler = [[BZTimerBlockHandler alloc] init];
    blockHandler.block = block;
    
    return [self scheduledTimerWithTimeInterval:seconds target:blockHandler selector:@selector(bz_handleBlockWithTimer:) userInfo:nil repeats:inRepeats];
}

+ (NSTimer *)bz_timerWithTimeInterval:(NSTimeInterval)seconds block:(HandleTimerBlock)block repeats:(BOOL)inRepeats{
    
    BZTimerBlockHandler *blockHandler = [[BZTimerBlockHandler alloc] init];
    blockHandler.block = block;
    
    return [self timerWithTimeInterval:seconds target:blockHandler selector:@selector(bz_handleBlockWithTimer:) userInfo:nil repeats:inRepeats];
    
}

@end



